package com.cn.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.domain.Order;
import com.cn.domain.User;
import com.cn.service.OrderService;
import com.cn.service.impl.OrderServiceImpl;
import com.cn.utils.PageView;

@WebServlet("/SearchAllOrderServlet")
public class SearchAllOrderServlet extends HttpServlet {
	
	private OrderService orderService=new OrderServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//当前页
		int currentPage=1;
		
		String pageNow=req.getParameter("pageNow");
		
		if(pageNow!=null){
			currentPage=Integer.parseInt(pageNow);
		}
		
		User user=(User) req.getSession().getAttribute("user");
		List<Order> queryOrdersPage = orderService.queryOrdersPage(user.getHu_user_id(), currentPage,PageView.PAGE_SIZE);
		
		if(user==null){
			//调到登录界面
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		int recordCount=orderService.queryOrdersByUserId(user.getHu_user_id());
		//计算总数量
		if(queryOrdersPage.size()==0||queryOrdersPage==null){
			//调到首页界面
			req.getRequestDispatcher("/index.jsp").forward(req, resp);
			return;
		}
		int sum=0;
		for(Order order:queryOrdersPage){
			sum+=order.getProduct().getHp_price()*order.getOrderDetail().getHod_quantity();
		}
		
		PageView pageModel=new PageView(currentPage,recordCount,queryOrdersPage);
		req.setAttribute("pageModel", pageModel);
		req.setAttribute("sum", sum);
		req.getRequestDispatcher("/all_orders_view.jsp").forward(req, resp);
	}
}
